//-----------------------------------------------------------------------
// <copyright file="NewsletterDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class NewsletterDAOBase
    {
        #region Common methods
        public virtual Newsletter CreateNewsletterFromReader(IDataReader reader)
        {
            Newsletter item = new Newsletter();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("NewsletterId"))) item.NewsletterId = (int)reader["NewsletterId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Content"))) item.Content = (string)reader["Content"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (int)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) item.UpdatedDate = (DateTime)reader["UpdatedDate"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateNewsletterFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateNewsletter methods
            
        public virtual void CreateNewsletter(Newsletter newsletter)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spNewsletterInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, newsletter.Title);
                database.AddInParameter(dbCommand, "@Content", DbType.String, newsletter.Content);
                database.AddInParameter(dbCommand, "@Order", DbType.Int32, newsletter.Order);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, newsletter.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, newsletter.UpdatedDate);
                database.AddOutParameter(dbCommand, "@NewsletterId", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                newsletter.NewsletterId = (int)database.GetParameterValue(dbCommand, "@NewsletterId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateNewsletterException, ex);
            }
        }

        #endregion

        #region UpdateNewsletter methods
        
        public virtual void UpdateNewsletter(Newsletter newsletter)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spNewsletterUpdate");            
                
                database.AddInParameter(dbCommand, "@NewsletterId", DbType.Int32, newsletter.NewsletterId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, newsletter.Title);
                database.AddInParameter(dbCommand, "@Content", DbType.String, newsletter.Content);
                database.AddInParameter(dbCommand, "@Order", DbType.Int32, newsletter.Order);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, newsletter.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, newsletter.UpdatedDate);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateNewsletterException, ex);
            }
        }
        
        #endregion

        #region DeleteNewsletter methods
        public virtual void DeleteNewsletter(int newsletterId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spNewsletterDelete");
                
                database.AddInParameter(dbCommand, "@NewsletterId", DbType.Int32, newsletterId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteNewsletterException, ex);
            }
        }

        #endregion

        #region GetNewsletter methods
        
        public virtual Newsletter GetNewsletter(int newsletterId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spNewsletterGet");
                
                database.AddInParameter(dbCommand, "@NewsletterId", DbType.Int32, newsletterId);
                
                Newsletter newsletter =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        newsletter = CreateNewsletterFromReader(reader);
                        reader.Close();
                    }
                }
                return newsletter;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetNewsletterException, ex);
            }
        }
        
        #endregion

        #region GetNewsletterList methods
        public virtual NewsletterCollection GetNewsletterList(NewsletterColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spNewsletterGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                NewsletterCollection newsletterCollection = new NewsletterCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Newsletter newsletter = CreateNewsletterFromReader(reader);
                        newsletterCollection.Add(newsletter);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return newsletterCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetNewsletterListException, ex);
            }
        }
        
        public virtual NewsletterCollection GetNewsletterList(NewsletterColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetNewsletterList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetNewsletterList methods
        public virtual NewsletterCollection GetNewsletterListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spNewsletterGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                NewsletterCollection newsletterCollection = new NewsletterCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Newsletter newsletter = CreateNewsletterFromReader(reader);
                        newsletterCollection.Add(newsletter);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return newsletterCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetNewsletterListException, ex);
            }
        }
        
        public virtual NewsletterCollection GetNewsletterListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetNewsletterListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}